package com.zhonglz.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhonglz.util.tools.UuidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhonglz.commons.base.BaseService;
import com.zhonglz.model.SysUser;
import com.zhonglz.service.SysUserService;
import com.zhonglz.util.Constans;
import com.zhonglz.util.jwt.JwtUtils;
import com.zhonglz.util.md5.Md5Utils;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.tools.ObjectUtils;

/***
 * 用户信息接口实现
 * @author admin
 *
 */
@Service
@Transactional 
public class SysUserServiceImpl extends BaseService implements SysUserService{

	/***
	 * 用户名密码登录
	 * 密码进行MD5以及盐值加密
	 */
	@Override
	public ResultUtils login(String username, String password) {
		ResultUtils result=new ResultUtils();
		try {
			//给原始密码做md5+盐值做加密
			String md5str = Md5Utils.getMd5Password(password);
			Map<String, Object> qmap=new HashMap<String, Object>();
			qmap.put("userName", username);
			qmap.put("userPwd", md5str);
			SysUser sysUser=sysUserMapper.selectByData(qmap);
			if(ObjectUtils.isEmpty(sysUser)) {
				result.setCode(ResultUtils.CODE_ERROR);
				result.setMsg("用户名或密码错误！");
				return result;
			}
			if(sysUser.getIsUse() != Constans.SYS_BASE_SET_IS_USE_TRUE) {
				result.setCode(ResultUtils.CODE_ERROR);
				result.setMsg("用户已被冻结！");
				return result;
			}

			//更新用户最后登录的时间
			sysUser.setLastLoginTime(new Date());
			sysUserMapper.updateByPrimaryKeySelective(sysUser);


			//结合用户名去数据库查询数据
			Map<String, String> map=new HashMap<String, String>();
			map.put("id", sysUser.getId().toString());
			map.put("userName", sysUser.getUserName());
			map.put("realName", sysUser.getRealName());
			map.put("userEmail", sysUser.getUserEmail());
			map.put("userPhone", sysUser.getUserPhone());
			map.put("isUse", sysUser.getIsUse().toString());
			map.put("vxOpenId", sysUser.getTokenId());
			String token = JwtUtils.getToken(map);
			map.put("token", token);
			result.setCode(ResultUtils.CODE_SUCCESS);
			result.setMsg("登陆成功!");
			result.setObject(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResultUtils.CODE_EXCEPTION);
			result.setMsg("请求出错，请稍后再试！");
		} 
		return result;
	}

	/***
	 * 根据条件查询出系统用户列表
	 */
	@Override
	public List<SysUser> getUserData(Map<String, Object> mapParam) {
		return sysUserMapper.selectByDataList(mapParam);
	}


	/***
	 * 保存用户信息
	 * @param sysUser
	 * @return
	 */
	@Override
	public ResultUtils saveUserInfo(SysUser sysUser) {
		if(ObjectUtils.isNotEmpty(sysUser.getId())){
			//给原始密码做md5+盐值做加密
			String md5str = Md5Utils.getMd5Password(sysUser.getUserPwd());
			sysUser.setUserPwd(md5str);
			int  count = sysUserMapper.updateByPrimaryKeySelective(sysUser);
			if(count>0){
				return new ResultUtils(ResultUtils.CODE_SUCCESS,"操作成功！",sysUser.getId());
			}else{
				return new ResultUtils(ResultUtils.CODE_ERROR,"操作失败！");
			}
		}else{
			String userId = UuidUtils.uuid();
			sysUser.setId(userId);
			//给原始密码做md5+盐值做加密
			String md5str = Md5Utils.getMd5Password(sysUser.getUserPwd());
			sysUser.setUserPwd(md5str);
			sysUser.setCreateTime(new Date());
			sysUser.setIsUse(Constans.SYS_STATE_ONE);
			int  count = sysUserMapper.insertSelective(sysUser);
			if(count>0){
				return new ResultUtils(ResultUtils.CODE_SUCCESS,"操作成功！",userId);
			}else{
				return new ResultUtils(ResultUtils.CODE_ERROR,"操作失败！");
			}
		}
	}

}
